import userService from "../model/userService";
import bcrypt from "bcrypt-nodejs";
import returnCode from "../common/returnCode";

export async function login(ctx) {
    let user = await userService.getUserByName(ctx.request.body.username)
    if (user && bcrypt.compareSync(ctx.request.body.password, user.password)) {
        console.log(bcrypt.compareSync(ctx.request.body.password, user.password))
        ctx.session.user = user
        returnCode.SUCCESS.returnObject = {
            name: user.name,
            role: user.Role.role,
            store: user.Store ? user.Store.name : null
        }
        ctx.body = returnCode.SUCCESS
    } else {
        ctx.body = returnCode.USERNAMEORPASSWORDERROR
    }
}
